<?php
exit();
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/15
 * Time: 11:03
 */

namespace app\api\model;


use think\Db;

class Payment
{
//支付成功处理==========
    public static function alipayApppaySuccess($order_no)
    {
        $order = Db::name('recharge');
        $info = $order->find(['order_no' => $order_no]);
        //订单状态判断
        if ($info['status'] == 1 || $info['status'] == -1){
            return ;
        }
        if ($info['type'] == 1) {
            //金币充值
            self::rechargeSuccess($info);
        } elseif ($info['type'] == 2) {
            //申请发包方
            self::initiatorSuccess($info);
        } elseif ($info['type'] == 3) {
            //申请推广员
            self::promoterSuccess($info);
        }
    }

    //申请脉大师
    private function promoterSuccess($info)
    {
        //支付成功,设为推广员,奖励上级直推推广员一份分红权
        $res = User::update(['is_promoter' => 1], ['id' => $user_id]);
        if (!$res) {
            return json(['errorCode' => 1, 'msg' => '申请失败']);
        }
        $promoter = UserModel::get($user['parentid']);
        RecommendModel::update(['is_promoter' => 1], ['user_id' => $user_id]);
        if ($promoter['is_promoter']) {
            UserModel::where('id', $promoter['id'])->setInc('bonus_weight');
        }

        //用户分红
        UserService::rechargeReward($user_id, $price, '申请【脉大师】');

        return json(['errorCode' => 0, 'msg' => '申请成功']);
    }

    //申请主编、小编
    private function initiatorSuccess($info)
    {
        //金币日志
        $log_data = [
            'title' => '申请【' . $level_str . '】赠送',
            'user_id' => $user_id,
            'task_id' => '',
            'type' => 1,
            'amount' => $give_gold,
            'add_time' => time()
        ];
        Db::name('gold')->insert($log_data);
        //增加金币
//        $res = UserModel::where('id', $user_id)->setInc('gold', $give_gold);
        $user->gold = $user['gold'] + $give_gold;
        $user->level = $level;
        $res = $user->save();

        //用户分红
        UserService::rechargeReward($user_id, $price, '申请【' . $level_str . '】');
    }

    //金币充值
    private function rechargeSuccess($info)
    {

    }

    //支付成功处理==========
}